package com.eduplatform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eduplatform.entity.BlacklistToken;
import com.eduplatform.mapper.BlacklistTokenMapper;
import com.eduplatform.service.BlacklistTokenService;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class BlacklistTokenServiceImpl extends ServiceImpl<BlacklistTokenMapper, BlacklistToken>
        implements BlacklistTokenService {

    @Override
    public boolean addToBlacklist(String token, Date expireTime) {
        try {
            BlacklistToken blacklistToken = new BlacklistToken();
            blacklistToken.setToken(token);
            blacklistToken.setExpireTime(expireTime);
            blacklistToken.setCreateTime(new Date());
            return this.save(blacklistToken);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean isInBlacklist(String token) {
        try {
            LambdaQueryWrapper<BlacklistToken> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(BlacklistToken::getToken, token);
            return this.count(queryWrapper) > 0;
        } catch (Exception e) {
            return false;
        }
    }
}